package com.example.controller;

import com.example.entity.Manager;
import com.example.entity.MyResponseEntity;
import com.example.entity.User;
import com.example.service.AuthService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 提供登录认证、授权
 *
 * @author fyx
 */
@RestController
public class AuthController {
    private final AuthService authService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @PostMapping("/login")
    public MyResponseEntity<?> login(@RequestBody User user) {
        User manager = authService.login(user.getUsername(), user.getPassword());
        if (manager == null) {
            return new MyResponseEntity<>(401, "登录失败", null);
        }
        return new MyResponseEntity<>(200, "登录成功", manager);
    }
}
